
#include "WebcamVideoPlayer.h"

/* Constructor
 * Sets up the layout of widget and paints a default image.
 */
WebcamVideoPlayer::WebcamVideoPlayer(QWidget *parent) : QWidget(parent) {
    layout = new QVBoxLayout;
    imagelabel = new QLabel;

    layout->addWidget(imagelabel);    
    image = QImage(640, 480,QImage::Format_RGB32);
    imagelabel->setPixmap(QPixmap::fromImage(image));



    setLayout(layout);
}

WebcamVideoPlayer::~WebcamVideoPlayer(void)
{
    //no dynamic memory, so we do nothing here.
}

/* convertToQImage
 * Takes as a parameter a pointer to an IplImage and then returns
 * a QImage. It does a pixel by pixel construction of the QImage,
 * which probaby isnt so efficient. Converts from BGR of IplImage
 * to the RGB of QImage
 */
QImage WebcamVideoPlayer::convertToQImage(IplImage *cvimage)
{
    int cvIndex, cvLineStart;
    // switch between bit depths
    if(cvimage->depth == IPL_DEPTH_8U)
    {
        if(cvimage->nChannels == 3)
        {
            if ( (cvimage->width != image.width()) || (cvimage->height != image.height()) )
            {
                QImage temp(cvimage->width, cvimage->height, QImage::Format_RGB32);
                image = temp;
            }
            cvIndex = 0; cvLineStart = 0;
            for (int y = 0; y < cvimage->height; y++)
            {
                unsigned char red,green,blue;
                cvIndex = cvLineStart;
                for (int x = 0; x < cvimage->width; x++)
                {
                    // swap colors around
                    red = cvimage->imageData[cvIndex+2];
                    green = cvimage->imageData[cvIndex+1];
                    blue = cvimage->imageData[cvIndex+0];
                    image.setPixel(x,y,qRgb(red, green, blue));
                    cvIndex += 3;
                }
                cvLineStart += cvimage->widthStep;
            }
        }
        else
        {
            std::cout << "This number of channels is not supported" << std::endl;
        }
    }
    else
    {
        std::cout << "This type of IplImage is not implemented in WebcamVideoPlayer" << std::endl;
    }

    return image;
}

/* PutImage method
 * Takes a pointer to a IplImage, converts the image to a QImage, and then
 * calls putImage with a QImage, which draws it to the screen.
 */
void WebcamVideoPlayer::putImage(IplImage *cvimage)
{
    QImage frame = convertToQImage(cvimage);
    putImage(&frame);
}

/* PutImage method (using Qimages)
 * Takes a pointer to a QImage and then draws it to in the widget.
 * It also takes an hSize variable, which is the size of the current widgets frame.
 * This is so that it can dynamically resize the video window while still maintaining
 * the aspect ratio. This method also mirrors the frame so that it looks like you are
 * looking into a mirror. (I.E your left hand is on the left hand side of the screen).
 */
void WebcamVideoPlayer::putImage(QImage* img)
{
    image = (*img);

}

void WebcamVideoPlayer::paintSubImage(QImage *pic, QRect pos)
{    
    painter = new QPainter(&image);
    painter->setOpacity(0.5);
    painter->drawImage(pos,(*pic));
    painter->end();
}

void WebcamVideoPlayer::paintWriting(QString text, QRect pos)
{
    painter = new QPainter(&image);
    QFont font = painter->font();
    font.setPixelSize(40);
    painter->setFont(font);
    painter->setPen(QColor(255,0,0));
    painter->drawText(pos,Qt::AlignCenter, text);
    painter->end();
}

void WebcamVideoPlayer::paintLabel(int size, bool resizebyHeight)
{
    //mirror horozontially
    //Image resizing
    imagelabel->setMinimumWidth(100);
    imagelabel->setMinimumHeight(100);
    if(resizebyHeight)
    {
        image = image.scaledToHeight(size, Qt::SmoothTransformation);
    }
    else
    {
        image = image.scaledToWidth(size, Qt::SmoothTransformation);
    }

    imagelabel->setAlignment(Qt::AlignCenter);

    //draw image to screen
    imagelabel->setPixmap(QPixmap::fromImage(image));
}

